package com.ybxx.base.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.dao.ResHydropowerDao;
import com.ybxx.base.dao.ResidentsDao;
import com.ybxx.base.dao.ResidentsMemberDao;
import com.ybxx.base.model.ResHydropower;
import com.ybxx.base.model.Residents;
import com.ybxx.base.model.ResidentsMember;
import com.ybxx.base.service.ResidentsService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.GenericDao;
import com.ybxx.core.service.impl.BaseServiceImpl;
import com.ybxx.core.web.paging.PagingBean;

public class ResidentsServiceImpl extends BaseServiceImpl<Residents> implements
		ResidentsService {
	private ResidentsDao residentsDao;
	private ResidentsMemberDao residentsMemberDao;
	private ResHydropowerDao resHydropowerDao;
	public ResidentsServiceImpl(ResidentsDao dao,ResidentsMemberDao residentsMemberDao,ResHydropowerDao resHydropowerDao) {
		super(dao);
		this.residentsDao=dao;
		this.residentsMemberDao=residentsMemberDao;
		this.resHydropowerDao=resHydropowerDao;
		// TODO Auto-generated constructor stub
	}
	public int checkConfig(String unitid){
		String hql="from Residents res right join res.room r " +
				"left join fetch r.unit u left join fetch u.ban b left join fetch b.community c left join fetch res.property h where res.rcode is null";
		if(unitid.indexOf("R")==0){
			hql+=" and r.rid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("U")==0){
			hql+=" and u.uid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("B")==0){
			hql+=" and b.bid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("C")==0){
			hql+=" and c.cid='"+unitid.substring(1)+"'";
		}
		return residentsDao.findCountByHql(hql, null);
	}
	public Object[] load(String rid,String rcode){
		String hql="from Residents res right join res.room r " +
				"left join fetch r.unit u left join fetch u.ban b left join fetch b.community c left join fetch res.property h where r.rid='"+rid+"'";
		if(StringUtils.isNotEmpty(rcode)){
			hql+=" and res.rcode='"+rcode+"'";
		}
		Object obj=this.residentsDao.findUnique(hql, null);
		return (Object[])obj;
	}
	public List<Residents> queryPage(QueryFilter filter, String cid,
			String bid, String uid, String rid, String loginid) {
		// TODO Auto-generated method stub
		String hql="select r,res from Residents res right join res.room as r left join fetch r.unit u " +
				"left join fetch u.ban b left join fetch b.community c,DataRights m " +
				"where r.rid=m.dvalue and m.dtype='R' and m.user.loginid='"+loginid+"'";
		if(StringUtils.isNotEmpty(cid)){
			hql+=" and r.unit.ban.community.cid like '%"+cid+"%'";
		}
		if(StringUtils.isNotEmpty(bid)){
			hql+=" and r.unit.ban.bid like '%"+bid+"%'";
		}
		if(StringUtils.isNotEmpty(uid)){
			hql+=" and r.unit.uid like '%"+uid+"%'";
		}
		if(StringUtils.isNotEmpty(rid)){
			hql+=" and r.rid like '%"+rid+"%'";
		}
		hql+=" order by c.cid,b.bname,u.uname,r.rcode";
		return this.residentsDao.findByHql(hql, null, filter.getPagingBean());
	}
	public void save(Residents residents, List<ResHydropower> hydropowers,
			ResidentsMember member) {
		String sql1="delete from tbl_base_residents_member where rcode='"+residents.getRcode()+"'";
		String sql2="delete from tbl_base_res_hydropower where rcode='"+residents.getRcode()+"'";
		residentsDao.excuteSql(sql1);
		residentsDao.excuteSql(sql2);
		residentsDao.save(residents);
		if(StringUtils.isNotEmpty(member.getMname())){
			residentsMemberDao.save(member);
		}
		for(ResHydropower hydropower:hydropowers){
			resHydropowerDao.save(hydropower);
		}
		
		
		
	}
	public void removeAll(String rcode) {
		String sql1="delete from tbl_base_residents_member where rcode='"+rcode+"'";
		String sql2="delete from tbl_base_res_hydropower where rcode='"+rcode+"'";
		residentsDao.excuteSql(sql1);
		residentsDao.excuteSql(sql2);
		residentsDao.remove(rcode);
		// TODO Auto-generated method stub
		
	}
	public List<Residents> loadAnmeterResidents(String rid) {
		String hql="from Residents res left join fetch res.room r " +
				"left join fetch r.unit u left join fetch u.ban b " +
				"left join fetch b.community where r.rid='"+rid+"' and res.state='0'";
		return residentsDao.findByHql(hql, null);
	}
	public Residents queryCurrentResidents(String rid){
		String hql="from Residents res left join fetch res.room r " +
		" where r.rid='"+rid+"' and res.state='0'";
        List<Residents> result=residentsDao.findByHql(hql, null);
        return (result!=null && result.size()>0)?result.get(0):null;
	}

}
